import { ArchivesList } from '@/services';
import { archivesTS } from '@/types';
import { Effect, Reducer } from 'umi';

export interface ArchivesModelState {
    archivesList: archivesTS[]
    archivesLength: number
    archivesData: object
}

export interface ArchivesModelType {
    namespace: 'archives';
    state: ArchivesModelState;
    effects: {
        ArchivesList: Effect;
    };
    reducers: {
        save: Reducer<ArchivesModelState>;
    };
}

const ArchivesModel: ArchivesModelType = {
    namespace: 'archives',

    state: {
        //归档列表数据
        archivesList: [],
        archivesLength: 0,
        archivesData: {},
    },

    effects: {
        // 归档列表
        *ArchivesList({ payload }, { call, put }) {
            const result = yield call(ArchivesList)
            // console.log(result.data)
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        archivesData: result.data,
                        archivesList: result.data[2022].March,
                        archivesLength: result.data[2022].March.length,
                    }
                })
            }
            // debugger
        },
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default ArchivesModel;